package br.com.livro.javapersistence.others.model;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;


/**
 * @author leo
 * 
 */
public class Message implements Serializable {

	private static final long serialVersionUID = 1815470032216829719L;
	private Long id;
	private String text;
	private Message nextMessage;
	private InfoDetail infoDetail;
	private MessageType messageType;
	private Collection<InfoDetail> comments = new HashSet<InfoDetail>();

	public Message() {
	}

	public Message(String text) {
		this.text = text;
	}

	public Message(String text, Message messageIndex) {
		this(text);
		this.nextMessage = messageIndex;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public Message getNextMessage() {
		return nextMessage;
	}

	public void setNextMessage(Message nextMessage) {
		this.nextMessage = nextMessage;
	}

	public InfoDetail getInfoDetail() {
		return infoDetail;
	}

	public void setInfoDetail(InfoDetail infoDetail) {
		this.infoDetail = infoDetail;
	}

	public String toString() {
		return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
	}

	public Collection<InfoDetail> getComments() {
		return comments;
	}

	public void setComments(Collection<InfoDetail> comments) {
		this.comments = comments;
	}

	/**
	 * @param infoDetail
	 * @throws IllegalArgumentException
	 *             for infoDetail null
	 */
	public void addComments(InfoDetail infoDetail) {
		if (this.getComments() == null)
			this.setComments(new HashSet<InfoDetail>());
		if (infoDetail == null)
			throw new IllegalArgumentException("InfoDetail can not be null");

		this.getComments().add(infoDetail);
	}

	public MessageType getMessageType() {
		return messageType;
	}

	public void setMessageType(MessageType messageType) {
		this.messageType = messageType;
	}

	/**
	 * @return
	 */
	public static Message getSimpleMessage(String messageText) {
		Message message = new Message(messageText);
		message.setInfoDetail(new InfoDetail(1111L, new Date(), "Adicionar"));
		message.addComments(new InfoDetail("Adicionando o contrato"));
		message.setMessageType(MessageType.MANUAL);
		return message;
	}

}
